package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.BitmapUtils;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.javaBean.CsOldAgePensionBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;

import java.util.List;

/**
 * 养老金签名
 * Created by ${wangtao} on 2017/3/6.
 */

public class PensionPublishSignActivity extends BaseActivity implements View.OnClickListener,ContributorView{

    private String uid;
    private int type;
    private int subType;
    private String cid;
    private CsOldAgePensionBean csOldAgePensionBean;
    private Context context;
    private ImageView regist_back;
    private TextView tv_headtitle;
    private TextView tv_sure;
    private MyToast myToast;
    private ContributorPresenter contributorPresenter;
    private TextView tv_clear;
    private SignaturePad mSignaturePad;
    private Bitmap photo;
    private final String UPLOADPHOTOFLAG="UPLOADPHOTOFLAG";
    private final String APPOINTMENTFLAG="APPOINTMENTFLAG";
    private String previousId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pensionpublishsign_layout);
        context=this;
        contributorPresenter = new ContributorPresenter(this, context);
        myToast=new MyToast(context);
        Intent intent=getIntent();
        uid=intent.getStringExtra("uid");
        type=intent.getIntExtra("type",10);
        subType=intent.getIntExtra("subType",0);
        cid=intent.getStringExtra("cid");
        previousId=intent.getStringExtra("previousId");
        csOldAgePensionBean=(CsOldAgePensionBean) intent.getSerializableExtra("object");
        initView();
    }

    private void initView()
    {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("请在以下区域签字");
        regist_back.setOnClickListener(this);
        tv_sure=(TextView) findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(this);
        tv_clear=(TextView) findViewById(R.id.tv_clear);
        tv_clear.setOnClickListener(this);
        mSignaturePad = (SignaturePad) findViewById(R.id.signature_pad);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_clear:
                mSignaturePad.clear();
                break;
            //审核提交
            case R.id.tv_sure:
                photo = mSignaturePad.getSignatureBitmap();
                photo=scaleBitmap(photo, Tools.dip2px(context,200),Tools.dip2px(context,100));
                String paramsOne= BitmapUtils.bitmapToBase64(photo);
                contributorPresenter.getUploadCommonPicList(paramsOne,UPLOADPHOTOFLAG);
                break;
        }
    }

    private Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
        if (origin == null) {
            return null;
        }
        int height = origin.getHeight();
        int width = origin.getWidth();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        if (!origin.isRecycled()) {
            origin.recycle();
        }
        return newBM;
    }



    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(UPLOADPHOTOFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    String jsonMessage=JsonTools.objectToJsonString(respObj.getMessage());
                    List<String> list=JsonTools.jsonStringToList(jsonMessage,String.class);
                    if(list!=null && list.size()>0)
                    {
                        csOldAgePensionBean.setSignImg(list.get(0));
                        if(previousId==null)
                            previousId="";
                        contributorPresenter.getCommunitySlSubmit(uid,cid,type,subType,JsonTools.objectToJsonString(csOldAgePensionBean),previousId,APPOINTMENTFLAG);
                    }

                }else
                {
                    myToast.showShort("提交签名图片失败");
                }
            } else if (flag.equals(APPOINTMENTFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    myToast.showShort("申请成功");
                    Intent intent=getIntent();
                    setResult(1,intent);
                    finish();
                }else
                {
                    myToast.showShort("操作失败");
                }
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(photo!=null && !photo.isRecycled())
        {
            photo.isRecycled();
            photo=null;
        }
    }

}
